package com.example.vertification.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class RedisUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private static final String QUESTIONNAIRE_PREFIX = "questionnaire:";
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public boolean setToken(String UUID, String UID) {
        stringRedisTemplate.opsForValue().set(UUID, UID, 60 * 60, TimeUnit.SECONDS);

        stringRedisTemplate.opsForValue().set(getUIDKey(UID), UUID, 60 * 60, TimeUnit.SECONDS);
        return true;
    }

    public boolean refreshTokenTime(String UUID) {
        String UID = getUIDByToken(UUID);
        if (UID != null) {
            stringRedisTemplate.opsForValue().set(UUID, UID, 60 * 60, TimeUnit.SECONDS);

            stringRedisTemplate.opsForValue().set(getUIDKey(UID), UUID, 60 * 60, TimeUnit.SECONDS);
        }
        return true;
    }

    public String getUIDByToken(String UUID) {
        return stringRedisTemplate.opsForValue().get(UUID);
    }


    public String getTokenByUID(String UID) {
        return stringRedisTemplate.opsForValue().get(getUIDKey(UID));
    }


    private String getUIDKey(String UID) {
        return "uid:" + UID;
    }





    // 设置IP的验证码冷却时间
    public void setIpCaptchaCooldown(String ip, String captcha, long cooldownDuration, TimeUnit unit) {
        // 使用特定的键名格式来区分token和IP验证码
        stringRedisTemplate.opsForValue().set(ip, captcha, cooldownDuration, unit);
    }


    // 获取IP的验证码（通常用于验证）
    public String getIpCaptcha(String ip) {
        // 使用特定的键名格式来区分token和IP验证码
        return stringRedisTemplate.opsForValue().get(ip);
    }

    public boolean canAnswerQuestion(String userId) {
        String today = DATE_FORMAT.format(new Date());
        String key = QUESTIONNAIRE_PREFIX + userId + ":" + today;
        String count =  String.valueOf(stringRedisTemplate.opsForHash().get(key, "count"));
        if (count == null || !count.equalsIgnoreCase("1")) {
            stringRedisTemplate.opsForHash().put(key, "count", "1");
            return true;
        } else {
            return false;
        }
    }

    @Scheduled(cron = "0 0 0 * * ?") // 每天午夜执行
    public void clearAnswers() {
        String today = DATE_FORMAT.format(new Date());
        stringRedisTemplate.keys(QUESTIONNAIRE_PREFIX + "*:" + today).forEach(key -> {
            stringRedisTemplate.delete(key);
        });
    }


    public boolean canPuzzle(String userId) {
        String today = DATE_FORMAT.format(new Date());
        String key = "PUZZLEGAME_" + userId + ":" + today;
        String count =  String.valueOf(stringRedisTemplate.opsForHash().get(key, "count"));
        if (count == null || !count.equalsIgnoreCase("1")) {
            return true;
        } else {
            return false;
        }
    }

    public void finalPuzzle(String userId) {
        String today = DATE_FORMAT.format(new Date());
        String key = "PUZZLEGAME_" + userId + ":" + today;
        stringRedisTemplate.opsForHash().put(key, "count", "1");
    }


    @Scheduled(cron = "0 0 0 * * ?") // 每天午夜执行
    public void clearPuzzle() {
        String today = DATE_FORMAT.format(new Date());
        stringRedisTemplate.keys("PUZZLEGAME_" + "*:" + today).forEach(key -> {
            stringRedisTemplate.delete(key);
        });
    }




}